// pages/giftCard/giftCardDetails/giftCardDetails.js
const giftCardService = require('../../../api/giftCardService.js')
const optionsService = require('../../../api/optionsService.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: "",
    beginDate: "",
    cardId: "",
    cardNo: "",
    created: "",
    endDate: "",
    imageUrl: "",
    memberCode: "",
    name: "",
    recharge: "",
    state: "",
    timeRange: "",
    remark: "",
    mobile: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.cardNo) {
      giftCardService.queryMyGiftCardDetails(options.cardNo).then(res => {
        console.log(res)
        if(res.remark) {
          this.setData({
            remark: res.remark.split('\n')
          })
        }
        if (res.myGiftCardDTO) {
          this.setData({
            balance: res.myGiftCardDTO.balance,
            beginDate: res.myGiftCardDTO.beginDate,
            cardId: res.myGiftCardDTO.cardId,
            cardNo: res.myGiftCardDTO.cardNo,
            created: res.myGiftCardDTO.created,
            endDate: res.myGiftCardDTO.endDate,
            imageUrl: res.myGiftCardDTO.imageUrl,
            memberCode: res.myGiftCardDTO.memberCode,
            name: res.myGiftCardDTO.name,
            recharge: res.myGiftCardDTO.recharge,
            state: res.myGiftCardDTO.state
          })
        }
        if(res.useRule) {
          this.setData({
            storeRange: res.useRule.storeRange
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
    // 查询商户电话号
    optionsService.query('appContactMobile').then(res => {
      if (res && res.length > 0) {
        res.forEach(item => {
          if (item.key === "appContactMobile") {
            this.setData({
              mobile: item.value
            })
          }
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 立即使用按钮点击事件
   */
  clickUse: function() {
    wx.navigateTo({
      url: '../giftCardPayQR/giftCardPayQR?cardNo=' + this.data.cardNo,
    })
  },

  /**
   * 点击电话拨打
   */
  handelCall: function() {
    wx.makePhoneCall({
      phoneNumber: this.data.mobile
    })
  },

  /**
   * 查看全部门店
   */
  toStoreList: function() {
    wx.setStorage({
      key: '_storeRange_',
      data: this.data.storeRange
    })
    wx.navigateTo({
      url: '../storeList/storeList',
    })
  }
})